package com.sky.service.user.impl;

import com.alibaba.fastjson.JSONObject;
import com.sky.constant.RedisConstant;
import com.sky.mapper.admin.DishFlavorsMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.user.UserDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.Result;
import com.sky.service.user.UserDishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserDishServiceImpl implements UserDishService {
    private final UserDishMapper userDishMapper;
    private final DishMapper dishMapper;
    private final DishFlavorsMapper dishFlavorsMapper;
    private final RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品列表
     * @param categoryId
     * @return
     */
    /*@Override
    public List<DishVO> list(Long categoryId) {
        log.info("根据分类id查询菜品列表，分类id为{}", categoryId);
        List<DishVO> dishVOList=userDishMapper.list(categoryId);
        return dishVOList;
    }*/

    /**
     * 根据套餐id查询菜品列表
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getById(Long setmealId) {
        log.info("根据套餐id查询菜品列表，套餐id为{}", setmealId);
        List<DishItemVO> dishItemVOList=userDishMapper.getById(setmealId);
        return dishItemVOList;
    }

    /**
     * 菜品查询
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        /**
         * 添加redis 缓存
         */
        //key 一定要加业务前缀 例：category:12
        String key=RedisConstant.CATEGORY_CACHE_PREFIX+ dish.getCategoryId();
        String dishJson = (String)redisTemplate.opsForValue().get(key);
        if (StringUtils.hasText(dishJson)){
            log.info("从redis缓存中获取菜品数据,{}",dishJson);
            return JSONObject.parseArray(dishJson,DishVO.class);
        }
        List<Dish> dishList=dishMapper.list(dish);
        List<DishVO> disVOList = new ArrayList<>();
        /**
         * 优化：使用stream流优化代码
         */
        //批量查询索引菜品的口味
        List<Long> dishIds = dishList.stream().map(Dish::getId).collect(Collectors.toList());
        List<DishFlavor> dishFlavors = dishFlavorsMapper.getFlavorByDishId(dishIds);
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors=dishFlavors.stream().filter(f->f.getDishId().equals(d.getId())).collect(Collectors.toList());
            dishVO.setFlavors(flavors);
            disVOList.add(dishVO);
        }
        redisTemplate.opsForValue().set(key,JSONObject.toJSONString(disVOList));
        return disVOList;
    }

}
